//
//  SegmentedProgressStyle.swift
//  Kosspot
//
//  Created by 李鑫 on 2025/7/29.
//

import SwiftUI
struct SegmentedProgressBar: View {
    let segments: [(value: CGFloat, color: Color)]
    let currentValue: CGFloat
    let height: CGFloat = 10
    let showWord:[String]
    var body: some View {
        GeometryReader { geometry in
            ZStack(alignment: .topLeading) {
                // 背景轨道
//                Capsule()
//                    .foregroundColor(.red)
//                    .frame(height: height)
                // 分段进度条
                HStack(spacing: 0) {
                    ForEach(segments.indices, id: \.self) { index in
                        let segment = segments[index]
                        let width = min(segment.value / totalValue * geometry.size.width, geometry.size.width)
                        VStack {
                            if index == 0 {
                                Rectangle()
                                    .foregroundColor(segment.color)
                                    .frame(width:(max(0, width-4)), height: height)
                                    .cornerRadius(5, corners: [.topLeft,.bottomLeft])
                            }else if index == segments.count-1 {
                                Rectangle()
                                    .foregroundColor(segment.color)
                                    .frame(width:(max(0, width-4)), height: height)
                                    .cornerRadius(5, corners: [.topRight,.bottomRight])
                            }else {
                                Rectangle()
                                    .foregroundColor(segment.color)
                                    .frame(width:(max(0, width-4)), height: height)
                            }
                            Spacer().frame(height: 12)
                            Text(showWord[index])
                                .font(.system(size: 12))
                                .foregroundStyle(Color(hex: "#B2B2B2"))
                                .frame(width: 50)
                        }
                        Spacer().frame(width: 4)
                    }
                }
                .padding(.top, 6)
                
                // 当前进度指示器
                let progressPosition = min(currentValue / totalValue * geometry.size.width, geometry.size.width)
                Image("progressIcon")
                    .foregroundColor(.white)
                    .offset(x: progressPosition - 10, y: 0)
                    .shadow(radius: 2)
            }
        }
        .frame(height: 40)
    }
    
    private var totalValue: CGFloat {
        segments.reduce(0) { $0 + $1.value }
    }
}
